package date0914;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
//修改subject的信息，相关信息由用户输入(update xxx set xx=ss where id=x)
public class UpdateSubject {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		Connection con = null;
		Statement sta = null;
		
		//加载驱动
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
		//接受键盘输入的修改属性
		System.out.println("请输入要修改属性");
		String att = input.next();
		System.out.println("请输入要修改后的属性值");
		String newAtt = input.next();
		System.out.println("请输入要更新科目id");
		int idd = input.nextInt();
		String sql= "update subject set " +  att + "=" + newAtt + " where id = " + idd + ";";
		
		//创建连接并执行更新操作
		try {
			con = DriverManager.getConnection("jdbc:mysql://localhost:3306/work","xjl","xjlroot");
			sta = con.createStatement();
			int r = sta.executeUpdate(sql);
			System.out.println("更新了" + r + "条记录");
			System.out.println("更新属性成功！！！");
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if(null != sta) {
				try {
					sta.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(null != con) {
				try {
					con.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
